package com.cherry.i18n.config.properties;

import com.cherry.i18n.enums.I18nCodeType;
import com.cherry.i18n.enums.MessageArgFormatType;
import com.cherry.i18n.response.I18nResponseAdvice;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Locale;

/**
 * 国际化消息
 *
 * @author sinbad.cheng
 * @since 2021-11-11
 */
@ConfigurationProperties(prefix = I18nProperties.PREFIX)
public class I18nProperties {

    public static final String PREFIX = "cherry.i18n";

    public static final String RESULT_CODE_PREFIX = "RC";

    /**
     * 是否启用国际化
     */
    private boolean enable = false;

    /**
     * Message Code类型
     */
    private I18nCodeType codeType = I18nCodeType.RESULT_MSG;

    /**
     * 默认语言（中文）
     */
    private Locale defaultLocale = Locale.SIMPLIFIED_CHINESE;

    /**
     * 参数格式化
     *
     * @see String#format(String, Object...)
     * @see java.text.MessageFormat
     */
    private MessageArgFormatType messageArgFormat = MessageArgFormatType.STRING_FORMAT;

    /**
     * 国际化请求头，默认：lang
     */
    private String localeHeader = "lang";

    /**
     * 是否启用响应内容解析，建议在小前台开启，中台服务不开启（综合考虑，暂时中台开启，小前台不开启）
     *
     * @see I18nResponseAdvice
     */
    private boolean enableResponseAdvice = true;

    /**
     * 本地缓存时间（单位秒，默认：5分钟）
     */
    private long localCacheTime = 300;

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public I18nCodeType getCodeType() {
        return codeType == null ? I18nCodeType.RESULT_MSG : codeType;
    }

    public void setCodeType(I18nCodeType codeType) {
        this.codeType = codeType;
    }

    public Locale getDefaultLocale() {
        return defaultLocale;
    }

    public void setDefaultLocale(Locale defaultLocale) {
        this.defaultLocale = defaultLocale;
    }

    public MessageArgFormatType getMessageArgFormat() {
        return messageArgFormat == null ? MessageArgFormatType.STRING_FORMAT : messageArgFormat;
    }

    public void setMessageArgFormat(MessageArgFormatType messageArgFormat) {
        this.messageArgFormat = messageArgFormat;
    }

    public String getLocaleHeader() {
        return localeHeader;
    }

    public void setLocaleHeader(String localeHeader) {
        this.localeHeader = localeHeader;
    }

    public boolean isEnableResponseAdvice() {
        return enableResponseAdvice;
    }

    public void setEnableResponseAdvice(boolean enableResponseAdvice) {
        this.enableResponseAdvice = enableResponseAdvice;
    }

    public long getLocalCacheTime() {
        return localCacheTime;
    }

    public void setLocalCacheTime(long localCacheTime) {
        this.localCacheTime = localCacheTime;
    }

    @Override
    public String toString() {
        return "I18nProperties{" +
                "enable=" + enable +
                ", codeType=" + codeType +
                ", defaultLocale=" + defaultLocale +
                ", messageArgFormat=" + messageArgFormat +
                ", localeHeader='" + localeHeader + '\'' +
                ", enableResponseAdvice=" + enableResponseAdvice +
                ", localCacheTime=" + localCacheTime +
                '}';
    }

}